package cn.ffcs.uec.staff.component;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.SuspendNotAllowedException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.staff.component.bean.UecRetireListboxBean;
import cn.ffcs.uec.staff.constant.UecStaffConstants;
import cn.ffcs.uec.staff.manager.UecRetireManager;
import cn.ffcs.uec.staff.model.UecRetire;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;
import lombok.Getter;
import lombok.Setter;
@Controller
@Scope("prototype")
public class UecRetireListboxExt extends Div implements IdSpace {

	private static final long serialVersionUID = -5811398149406451671L;
	/**
	 * 人员基本信息功能 查询出的实体对象
	 */
	private UecStaffInfo uecStaffInfo;
	/**
	 * 对应页面上的bean
	 */
	private UecRetireListboxBean bean = new UecRetireListboxBean();
	/**
	 * 人员处罚情况实体
	 */
	private UecRetire uecRetire;
	/**
	 * 人员处罚情况查询实体
	 */
	 private UecRetire qryUecRetire;

	private UecRetireManager uecRetireManager = (UecRetireManager) ApplicationContextUtil
			.getBean("uecRetireManager");

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecRetireListboxExt() {
		Executions.createComponents(UecStaffConstants.UEC_RETIRE_EXT_LISTBOX_ZUL,
				this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');

		this.addForward(UecStaffConstants.ON_UEC_RETIRE_QUERY, this,
				UecStaffConstants.ON_UEC_RETIRE_QUERY_RESPONSE);

		this.addForward(UecStaffConstants.ON_CLEAN_UEC_RETIRE, this,
				UecStaffConstants.ON_CLEAN_UEC_RETIRE_RESPONSE);

	}

	/**
	 * 初始化
	 */
	public void onCreate() {
		this.setButtonValid(false, false, false, false);
	}

	/**
	 * 选择查询记录
	 * 
	 * @throws Exception
	 */
	public void onSelectUecRetireList() throws Exception {
		if (this.bean.getUecRetireListbox().getSelectedCount() > 0) {
			uecRetire = (UecRetire) bean.getUecRetireListbox().getSelectedItem()
					.getValue();
		}
	}

	/**
	 * ，抛出事件， 事件监听
	 * 
	 * @param event
	 */
	public void onUecRetireQueryResponse(ForwardEvent event) {
		uecStaffInfo = (UecStaffInfo) event.getOrigin().getData();
		if (null != uecStaffInfo) {
			if (!StrUtil.isEmpty(uecStaffInfo.getPkPsndoc())) {//判断是否为集团数据
				this.setButtonValid(true, false, false,false);
			}else {
				this.setButtonValid(true, true,true,true);
			}
			qryUecRetire = new UecRetire();
			if (!StrUtil.isEmpty(uecStaffInfo.getPkPsndoc())) {
				qryUecRetire.setPkPsndoc(uecStaffInfo.getPkPsndoc());
			}
			qryUecRetire.setStaffId(uecStaffInfo.getStaffId());
			this.onQueryUecRetire();
		}
	}
	/**
	 * 清除
	 * 
	 * @param event
	 */
	public void onCleanUecRetireResponse(ForwardEvent event) {
		clearVariable();
		uecStaffInfo = null;
		ListboxUtils.clearListbox(this.bean.getUecRetireListbox());
		this.bean.getUecRetireListboxPaging().setTotalSize(1);
	}

	/**
	 * 设置按钮
	 * 
	 * @param canAdd
	 * @param candel
	 */
	private void setButtonValid(boolean canView, boolean canAdd,
			boolean canEdit, boolean candel) {
		this.bean.getViewButton().setDisabled(!canView);
		this.bean.getAddButton().setDisabled(!canAdd);
		this.bean.getEditButton().setDisabled(!canEdit);
		this.bean.getDelButton().setDisabled(!candel);
	}

	/**
	 * 查询按钮
	 */
	public void onQueryUecRetire() {
		this.bean.getUecRetireListboxPaging().setActivePage(0);
		this.onQueryUecRetireList();
	}

	/**
	 * 重置按钮
	 */
	public void onResetUecRetire() {
		ListboxUtils.clearListbox(this.bean.getUecRetireListbox());
		this.bean.getUecRetireListboxPaging().setTotalSize(1);
	}
	/**
	 * 查看按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onViewUecRetire() throws Exception, InterruptedException {
		openUecEditWin("view");
	}

	/**
	 * 新增按钮
	 * 
	 * @throws Exception
	 * @throws
	 */
	public void onAddUecRetire() throws Exception {
		openUecEditWin("add");
	}
	/**
	 * 编辑按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onEditUecRetire() throws Exception, InterruptedException {
		openUecEditWin("edit");
	}
	/**
	 * 
	 * @param opType
	 */
	private void openUecEditWin(String opType) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("opType", opType);
		if ("view".equals(opType) || "edit".equals(opType)) {
			if (null != uecRetire) {
				map.put("uecRetire", uecRetire);
			} else {
				ZkUtil.showError("请选择一条记录！", "提示信息");
				return;
			}
		} else if ("add".equals(opType)) {
			map.put("uecStaffInfo", uecStaffInfo);
		}
		Window window = (Window) Executions.createComponents(
				UecStaffConstants.UEC_RETIRE_EDIT_ZUL, this, map);
		try {
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						// 新增或修改完成后，列表从数据库更新
						uecRetire = (UecRetire) event.getData();
						onQueryUecRetireList();
					}
				}
			});
		} catch (SuspendNotAllowedException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 删除按钮
	 */
	public void onDelUecRetire() {
		if (this.uecRetire != null && uecStaffInfo != null) {
			ZkUtil.showQuestion("确定要删除吗?", "提示信息", new EventListener() {
				public void onEvent(Event event) throws Exception {
					Integer result = (Integer) event.getData();
					if (result == Messagebox.OK) {
						uecRetire.removeOnly();
						PubUtil.reDisplayListbox(bean.getUecRetireListbox(),
								uecRetire, "del");
						uecRetire = null;
					}
				}
			});
		} else {
			ZkUtil.showError("请选择你要删除的记录", "提示信息");
			return;
		}
	}

	/**
	 * 翻页
	 */
	public void onUecRetireListboxPaging() {
		this.onQueryUecRetireList();
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecRetire = null;
	}

	/**
	 * 查询
	 */
	public void onQueryUecRetireList() {
		if (null != qryUecRetire) {
			PageInfo pageInfo = uecRetireManager.queryPageInfoByUecRetire(qryUecRetire,
					this.bean.getUecRetireListboxPaging().getActivePage() + 1,
					this.bean.getUecRetireListboxPaging().getPageSize());
			ListModel list = new BindingListModelList(pageInfo.getDataList(),
					true);
			this.bean.getUecRetireListbox().setModel(list);
			this.bean.getUecRetireListboxPaging().setTotalSize(
					pageInfo.getTotalCount());
		}
	}

}
